package com.blog.message.api.config;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.blog.message.api.constant.QueueConstant.*;

/**
 * <p>
 * 消息队列配置
 * </p>
 *
 * @author Valerie
 * @date 2023/5/27
 **/
@Configuration
@EnableRabbit
public class MessageConfig {

    @Bean
    public Queue commentQueue(){
        return new Queue(COMMENT_QUEUE, true, false, false, getArgs());
    }

    @Bean
    public TopicExchange topicExchange(){
        Map<String, Object> args = new HashMap<>(16);
        args.put("x-message-ttl", 10000);
        return new TopicExchange(TOPIC_EXCHANGE, false, false, args);
    }

    @Bean
    public Binding bindingTopicExchange(){
        return BindingBuilder.bind(commentQueue()).to(topicExchange()).with(COMMENT_KEY);
    }

    @Bean
    public DirectExchange deadExchange(){
        //参数介绍
        //1.交换器名 2.是否持久化 3.自动删除 4.其他参数
        return new DirectExchange(DEAD_EXCHANGE,false,false,null);
    }

    @Bean
    public Queue directQueue(){
        //参数介绍
        //1.队列名 2.是否持久化 3.是否独占 4.自动删除 5.其他参数
        return new Queue(DEAD_QUEUE,false,false,false,null);
    }

    @Bean
    public Queue configQueue(){
        return new Queue(CONFIG_QUEUE,true,false,false,null);
    }


    @Bean
    public Map<String, Object> getArgs(){
        Map<String, Object> args = new HashMap<>(3);
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "dead_exchange");
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "dead");
        return args;
    }


}
